import { getMongoRepository } from "typeorm";

import { Students } from '../models/Student'
import classesService from "./classesService";

class StudentService {
  // 查询全部数据，分页（废弃）
  find(skipNum: number, limit: number) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.find({
      skip: skipNum,
      take: limit
    })
  }

  // 查询全部数据
  findAll() {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.find()
  }

  // 根据学号查询 student 用户
  async findOneByStu_num(stu_num: string) {
    const studentRepository = getMongoRepository(Students);
    const student = await studentRepository.findOne({ stu_num })
    if (student?.class_id) {
      const classes = await classesService.findById(student.class_id)
      student.classes = classes
    }
    return student
    // return studentRepository.findOne({ stu_num })

  }

  // 保存 student 用户
  async save(student: Students) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.save(student)
  }

  // 根据 id 查询 student 用户
  async findById(id: string) {
    const studentRepository = getMongoRepository(Students);
    const student = await studentRepository.findOne(id)
    if (student?.class_id) {
      const classes = await classesService.findById(student.class_id)
      student.classes = classes
    }
    return student
  }

  // 查询 student 用户总数量
  async studentCount() {
    const studentRepository = getMongoRepository(Students);
    const [users, count] = await studentRepository.findAndCount()
    return count
  }

  // 更新头像
  updateAvatar(id: string, avatar: string) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.update(id, { avatar })
  }

  // 根据手机号码查询用户
  findByPhone(phone: string) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.findOne({ phone })
  }

  // 修改密码
  updatePassWord(id: string, password: string) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.update(id, { password })
  }
  // 修改用户状态
  updateState(id: string, state: boolean) {
    const adminRepository = getMongoRepository(Students);
    return adminRepository.update(id, { state })
  }

  // 修改信息
  updateInfo(id: string, student: Students) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.update(id, student)
  }

  //*==================================================================
  // TODO 1. 根据名字模糊查询，分页
  findByNameKey(name: string, skipNum: number, limit: number) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.find({
      where: {
        name: { $regex: name }
      },
      skip: skipNum,
      take: limit
    })
  }
  // 根据名字模糊查询，总数量
  async findByNameKeyCount(name: string) {
    const studentRepository = getMongoRepository(Students);
    const [users, count] = await studentRepository.findAndCount({
      where: {
        name: { $regex: name }
      }
    })
    return count
  }
  //*==================================================================
  // TODO 2. 根据专业模糊查询，分页
  findByMajorKey(major: string, skipNum: number, limit: number) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.find({
      where: {
        major: { $regex: major }
      },
      skip: skipNum,
      take: limit
    })
  }
  // 根据专业模糊查询，总数量
  async findByMajorKeyCount(major: string) {
    const studentRepository = getMongoRepository(Students);
    const [users, count] = await studentRepository.findAndCount({
      where: {
        major: { $regex: major }
      }
    })
    return count
  }
  //*==================================================================
  // TODO 3. 根据学院模糊查询，分页
  findByCollegeKey(college: string, skipNum: number, limit: number) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.find({
      where: {
        college: { $regex: college }
      },
      skip: skipNum,
      take: limit
    })
  }
  // 根据学院模糊查询，总数量
  async findByCollegeKeyCount(college: string) {
    const studentRepository = getMongoRepository(Students);
    const [users, count] = await studentRepository.findAndCount({
      where: {
        college: { $regex: college }
      }
    })
    return count
  }
  //*==================================================================

  // 根据id进行删除
  deleteById(id: string) {
    const studentRepository = getMongoRepository(Students)
    return studentRepository.delete(id)
  }

  // 创建多个用户
  saveMany(students: Students[]) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.save(students)
  }

  // 根据班级查询学生
  findStuByClass(classes: string) {
    const studentRepository = getMongoRepository(Students);
    return studentRepository.findOne({ class: classes })
  }

}

export default new StudentService()